package com.aimei.www.moduel.util;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author xiah
 * @since
 */
public class VerifyCodeGenerator {

       public static final String number = "0123456789";
       public static final String characters = "0123456789";
       public static final char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
               'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
               'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

       public static final VerifyCodeGenerator verifyCodeGenerator = new VerifyCodeGenerator();

       private VerifyCodeGenerator() {
       }

       public static VerifyCodeGenerator getInstance() {
              if ( verifyCodeGenerator == null ) {
                     synchronized (number) {
                            if ( verifyCodeGenerator == null ) {
                                   return new VerifyCodeGenerator();
                            } else {
                                   return verifyCodeGenerator;
                            }
                     }
              } else {
                     return verifyCodeGenerator;
              }

       }


       /**
        * 生成指定长度的随机数 纯数字组合
        *
        * @param length
        * @return String
        */
       public static String generatorRandomNumber(int length) {
              StringBuilder sb = new StringBuilder(length);
              ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
              for ( int i = 0; i < length; ++i ) {
                     int rand = threadLocalRandom.nextInt(number.length());
                     sb.append(number.charAt(rand));
              }
              return sb.toString();
       }


       /**
        * 生成指定长度的随机数 纯字母组合
        *
        * @param length
        * @return String
        */
       public static String generatorRandomCharacters(int length) {
              StringBuilder sb = new StringBuilder(length);
              ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
              for ( int i = 0; i < length; ++i ) {
                     int rand = threadLocalRandom.nextInt(characters.length());
                     sb.append(characters.charAt(rand));
              }
              return sb.toString();
       }


       /**
        * 生成指定长度的随机数  字母数字组合,为防止o与0混淆，删除了o
        *
        * @param length
        * @return String
        */
       public static String genRandomNum(int length) {
              int maxNum = 35;
              int i;
              int count = 0;

              StringBuffer pwd = new StringBuffer("");
              Random r = new Random();
              while ( count < length ) {
                     i = Math.abs(r.nextInt(maxNum));
                     if ( i >= 0 && i < str.length ) {
                            pwd.append(str[i]);
                            count++;
                     }
              }
              return pwd.toString();
       }

}
